﻿#region
using System;
using System.Collections;
using System.Data;
using ZhongLangDB;
#endregion

namespace ZHONGLANGWEBUI.ZHONGLANG.SYS
{
    public partial class DEPT : TT_BasePageList
    {
        protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { pageInit(); dgListDo("查询初次", ""); } }

        #region pageInit
        protected override void pageInit()
        {
            DataTable dtCom = proGETCOM();
            DDLBind(dtCom, "COMCODENAME", "COMID", true, ref ddlQCOMID);  DDLBindValue(CURREMP.COMID, ref ddlQCOMID); ddlQCOMID_OnSelectedIndexChanged(null,null);
            DDLBind(dtCom, "COMCODENAME", "COMID", false, ref ddlECOMID); DDLBindValue(CURREMP.COMID, ref ddlECOMID); ddlECOMID_OnSelectedIndexChanged(null, null);

            DDLBindStr("1$正常,9$停用", true, ref ddlQDEPTSTATE);
            DDLBindStr("1$正常,9$停用", false, ref ddlEDEPTSTATE);
        }
        protected void ddlQCOMID_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlQCOMID.SelectedValue == "") { return; }
            DataTable dtDEPTPRO = proGETDEPTPRO(ddlQCOMID.SelectedValue);
            DDLBind(dtDEPTPRO, "DEPTCODENAME", "DEPTID", true, ref ddlQDEPTPRO); DDLBindValue(CURREMP.DEPTPRO, ref ddlQDEPTPRO);
        }
        protected void ddlECOMID_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlECOMID.SelectedValue == "") { return; }
            DataTable dtDEPTPRO = proGETDEPTPRO(ddlECOMID.SelectedValue);
            DDLBind(dtDEPTPRO, "DEPTCODENAME", "DEPTID", true, ref ddlEDEPTPRO); ddlEDEPTPRO.Items[0].Text = "无"; DDLBindValue(CURREMP.DEPTPRO, ref ddlEDEPTPRO);
        }
        #endregion

        #region pageBind 
        protected DataTable proSDListQuery(int iType)
        {
            SortedList SDList = new SortedList();
            SDList["COMID"] = ddlQDEPTPRO.SelectedValue;
            SDList["DEPTPRO"] = ddlQDEPTPRO.SelectedValue;
            SDList["DEPTNAME"] = txtQDEPTNAME.Text;
            SDList["DEPTSTATE"] = ddlQDEPTSTATE.SelectedValue;
            DataTable dt = pgShow.proQueryData(SDList, "PROTT_SYS_DEPTQUERY", iType);
            return dt;
        }

        protected override void pageBind()
        {
            DataTable dt = proSDListQuery(1);
            DataView dv = dt.DefaultView;
            if (ViewState["SortExpression"] != null) { dv.Sort = GetSortDirectiont(ViewState["SortExpression"].ToString(), ViewState["SortDirection"].ToString()); }
            dgList.DataSource = dv; dgList.DataBind();
        }
        #endregion

        #region pageOpt 
        protected override void dgListDo(string strCommandName, string strCommandArgument)
        {
            #region dgBase 
            if (strCommandName == "查询初次") { pgShow.CurrPageIndex = 1; pgShow.CurrPageSize = REQPAGECOUNT; pageBind(); }
            if (strCommandName == "查询") { pageBind(); tabQuery.Visible = true; tabShow.Visible = true; tabEdit.Visible = false; }
            if (strCommandName == "导出") { proExpData(proSDListQuery(2), dgList); }
            if (strCommandName == "关闭") { tabQuery.Visible = true; tabShow.Visible = true; tabEdit.Visible = false; }
            if (strCommandName == "添加") { txtEDEPTNAME.Text = ""; txtEDEPTCODE.Text = ""; txtEDEPTREMARK.Text = ""; lblUpdate.Text = ""; btnSave.CommandArgument = ""; tabQuery.Visible = false; tabShow.Visible = false; tabEdit.Visible = true; }
            #endregion
            
            #region 修改 
            if (strCommandName == "修改")
            {
                SortedList SDList = new SortedList();
                SDList["KEYID"] = strCommandArgument;
                DataTable dt = DBPark.proGetDT(SDList, "PROTT_SYS_DEPTQUERY");
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow dr = dt.Rows[0];
                    DDLBindValue(dr["COMID"].ToString(), ref ddlECOMID);
                    DDLBindValue(dr["DEPTPRO"].ToString(), ref ddlEDEPTPRO);
                    txtEDEPTCODE.Text = dr["DEPTCODE"].ToString();
                    txtEDEPTNAME.Text = dr["DEPTNAME"].ToString();
                    txtEDEPTMANAGE.Text = dr["DEPTMANAGE"].ToString();
                    txtEDEPTREMARK.Text = dr["DEPTREMARK"].ToString();
                    DDLBindValue(dr["DEPTSTATE"].ToString(), ref ddlEDEPTSTATE);
                     
                    txtEditor.Text = DBPark.txtFile.txtReadFromFile(CURRPATH(), dr["DEPTXML"].ToString(), "COMDEPTXML");
                    hidEditorPath.Value = dr["DEPTXML"].ToString();

                    lblUpdate.ForeColor = System.Drawing.Color.Blue;
                    lblUpdate.Text = dr["UPDATEMANNAME"].ToString() + "【" + strDateFmtTime(dr["UPDATETIME"].ToString()) + "编辑】";
                    btnSave.CommandArgument = strCommandArgument; tabQuery.Visible = false; tabEdit.Visible = true; tabShow.Visible = false;

                }
                else { jsMessage("参数有误，未找到对应记录！"); }
            }
            #endregion

            #region 保存
            if (strCommandName == "保存")
            {
                SortedList SDList = new SortedList();
                SDList["COMID"] = ddlECOMID.SelectedValue;
                SDList["DEPTPRO"] = ddlEDEPTPRO.SelectedValue;
                SDList["DEPTCODE"] = txtEDEPTCODE.Text;
                SDList["DEPTNAME"] = txtEDEPTNAME.Text;
                SDList["DEPTMANAGE"] = txtEDEPTMANAGE.Text;
                SDList["DEPTREMARK"] = txtEDEPTREMARK.Text;
                SDList["DEPTSTATE"] = ddlEDEPTSTATE.SelectedValue;
                SDList["DEPTXML"] = (btnSave.CommandArgument == "" ? "DEPTXML_" + ddlECOMID.SelectedItem.Text.Split(' ')[0] + "_" + SDList["DEPTCODE"].ToString() + DateTime.Now.ToString("yyMMddHHmmss") + ".htm" : hidEditorPath.Value);

                SDList["KEYID"] = btnSave.CommandArgument; SDList["UPDATEMANID"] = CURREMP.USERID; SDList["UPDATEMANNAME"] = CURREMP.USERNAME;
                string strBack = DBPark.proSetDB(SDList, "PROTT_SYS_DEPTSAVE");
                if (strBack != "") { lblUpdate.Text = strBack; lblUpdate.ForeColor = System.Drawing.Color.Red; jsMessage(strBack); }
                else {
                    DBPark.txtFile.txtSaveToFile(txtEditor.Text, CURRPATH(), SDList["DEPTXML"].ToString(), "COMDEPTXML", true);
                    dgListDo("查询", ""); }
            }
            #endregion 
        }
        #endregion 
    }
}